//全局网站信息
import {defineStore} from 'pinia';
import {api} from '@/utils/api';

interface WebsiteInfoState {
    title: string;
    icon: string | undefined;
    logPic: string | undefined;
    bulletin: string;
    monthly: string;
    realTime: string;
    subPic: string | undefined;
    copyright: string;
    codePic: string | undefined;
}

export const useWebsiteInfoStore = defineStore('websiteInfo', {
    persist: true,
    state: (): WebsiteInfoState => ({
        title: '',
        icon: '',
        logPic: '',
        bulletin: '',
        monthly: '',
        realTime: '',
        subPic: '',
        copyright: '',
        codePic: '',
    }),
    actions: {
        async fetchWebsiteInfo() {
            try {
                const websiteInfo = await api.getWebsiteInformation();
                if (websiteInfo && websiteInfo.code === 1) {
                    const {
                        title,
                        icon,
                        logPic,
                        bulletin,
                        monthly,
                        realTime,
                        subPic,
                        copyright,
                        codePic
                    } = websiteInfo.data;
                    this.title = title;
                    this.icon = await api.getResourceByPath(icon);
                    this.logPic = await api.getResourceByPath(logPic);
                    this.bulletin = bulletin;
                    this.monthly = monthly;
                    this.realTime = realTime;
                    this.subPic = await api.getResourceByPath(subPic);
                    this.copyright = copyright;
                    this.codePic = await api.getResourceByPath(codePic);
                } else {
                    console.error('Failed to fetch website information:', websiteInfo?.msg || 'Unknown error');
                }
            } catch (error) {
                console.error('Error loading website information:', error);
            }
        },
        async fetchQrCode() {
            try {
                const article = await api.getNewsCenterById(11);
                if (article && article.data.pic) {
                    this.codePic = await api.getResourceByPath(article.data.pic);
                }
            } catch (error) {
                console.error('Error loading article information:', error);
            }
        },
    },
});